import { request } from '../../request/index.js'
import regeneratorRuntime from '../../lib/runtime/runtime.js'

// pages/goods_detail/goods_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj: {}
  },
  GoodsInfo: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {goods_id} = options
    this.getGoodsDetail(goods_id)
  },

  async getGoodsDetail(goods_id) {
    const goodsObj = await request({
      url: "/goods/detail",
      data: {goods_id}
    })
    this.GoodsInfo = goodsObj
    this.setData({
      goodsObj
    })
  },

  handlePreviewImage(e) {
    const urls = this.GoodsInfo.pics.map(v => v.pics_mid)
    const current = e.currentTarget.dataset.url
    wx.previewImage({
      current,
      urls
    })
  },
  
  handleCartAdd() {
    let cart = wx.getStorageSync("cart") || [];
    let index = cart.findIndex(v => v.goods_id == this.GoodsInfo.goods_id)
    if (index == -1) {
      this.GoodsInfo.num = 1
      cart.push( this.GoodsInfo )
    } else {
      cart[index].num++
    }

    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: 'success',
      icon: 'success',
      mask: true,
    })
  }
})